package com.lancoo.znbkxx.teacher.aibkTestCoach.paperToEvaluation

import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.PaperEvaluationInfor
import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerAdapter
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerViewHolder
import com.lancoo.znbkxx.teacher.R
import kotlinx.android.synthetic.main.aibk_adapter_answersheet_child.view.*

/**
 * auther: 王国龙
 * Date：2020.06.15
 * describe:试卷预览对应的答题卡
 */
class AibkEvaluationAnswerSheetChildAdapter(
        val context: Context,
        val paperEvaluationInfor: PaperEvaluationInfor,
        var index_A: Int
) : KBaseRecyclerAdapter() {

    override fun getHolderView(parent: ViewGroup?, position: Int): View {
        return LayoutInflater.from(context)
                .inflate(R.layout.aibk_adapter_answersheet_child, parent, false)
    }

    override fun onBindViewHolder(
            holder: KBaseRecyclerViewHolder,
            position: Int
    ) {
        val indexB = position_return_index_B(paperEvaluationInfor, position)
        val indexC = position_return_index_C(paperEvaluationInfor, position)

        val sortIndex = paperEvaluationInfor.TypeList[index_A].QuesList[indexB]
                .SortScoreRateList[indexC].SortIndex
        val scoreRate = paperEvaluationInfor.TypeList[index_A].QuesList[indexB]
                .SortScoreRateList[indexC].ScoreRate
        holder.convertView.BtnSortIndex.text = sortIndex

        var scoreRateX = 0.0
        if(scoreRate == "--"){
            scoreRateX = 0.0
        }else{
            scoreRateX = scoreRate.toDouble()
        }
        if (scoreRateX >= 0.85) {
            holder.convertView.BtnSortIndex.setBackgroundResource(R.mipmap.aibk_icon_choice_green)
        } else if (scoreRateX >= 0.7 && scoreRateX < 0.85) {
            holder.convertView.BtnSortIndex.setBackgroundResource(R.mipmap.aibk_icon_choice_blue)
        } else if (scoreRateX >= 0.6 && scoreRateX < 0.7) {
            holder.convertView.BtnSortIndex.setBackgroundResource(R.mipmap.aibk_icon_choice_orange)
        } else {
            holder.convertView.BtnSortIndex.setBackgroundResource(R.mipmap.aibk_icon_choice_red)
        }
        holder.convertView.BtnSortIndex.setTextColor(context.resources.getColor(R.color.lancoooWhite))

        holder.convertView.BtnSortIndex.setOnClickListener {
            var indexC = position_return_index_C(paperEvaluationInfor, position)
            clickChilidListener!!.onClick(index_A, indexB, indexC)
        }

    }

    override fun getItemCount(): Int {
        var itemCount = 0
        var quesListSize = paperEvaluationInfor.TypeList[index_A].QuesList.size
        for (b in 0 until quesListSize) {
            itemCount += paperEvaluationInfor.TypeList[index_A].QuesList[b].SortScoreRateList.size
        }
        return itemCount
    }

    interface OnClickChildListener {
        fun onClick(index_A: Int, index_B: Int, index_C: Int)
    }

    private var clickChilidListener: OnClickChildListener? = null

    fun setClickChildListener(clickChilidListener: OnClickChildListener?) {
        this.clickChilidListener = clickChilidListener
    }

    private fun position_return_index_B(paperEvaluationInfor: PaperEvaluationInfor, position: Int): Int {
        var postionX = -1
        var indexB = 0
        var quesListSize = paperEvaluationInfor.TypeList[index_A].QuesList.size
        outLoop@ for (b in 0 until quesListSize) {
            var sortIndexListSize = paperEvaluationInfor.TypeList[index_A].QuesList[b]
                    .SortScoreRateList.size
            for (c in 0 until sortIndexListSize) {
                postionX += 1
                if (position == postionX) {
                    indexB = b
                    break@outLoop
                }
            }
        }
        return indexB
    }

    private fun position_return_index_C(paperEvaluationInfor: PaperEvaluationInfor, position: Int): Int {
        var postionX = -1
        var indexC = 0
        var quesListSize = paperEvaluationInfor.TypeList[index_A].QuesList.size
        outLoop@ for (b in 0 until quesListSize) {
            var sortIndexListSize = paperEvaluationInfor.TypeList[index_A].QuesList[b]
                    .SortScoreRateList.size
            for (c in 0 until sortIndexListSize) {
                postionX += 1
                if (position == postionX) {
                    indexC = c
                    break@outLoop
                }
            }
        }
        return indexC
    }

}